package tech.aistar.corejava.day17.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:反射调用构造方法
 * @date 2019/8/7 0007
 */
public class ConstructorDemo {
    public static void main(String[] args) {
        Class<?> c = Point.class;

        //1 - 调用空参构造
        try {
            Point p = (Point) c.newInstance();

            //2. 获取Constructor实例
            Constructor<?>[] arr = c.getConstructors();

            for (Constructor<?> constructor : arr) {
                System.out.println(constructor);
            }

            //根据指定的参数来获取
            Constructor<?> c1 = c.getConstructor();
            Point pp = (Point) c1.newInstance();//调用空参构造

            System.out.println("======");

            Constructor<?> c2 = c.getConstructor(int.class,String.class);

            Point p3 = (Point) c2.newInstance(12,"tom");

            System.out.println(p3);


        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
